/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "LOGR_gen_pages.h"
#include "LOGR_independent_pages.h"
#include "LOGR_independent_links.h"
#include "LOGR_gen_links.h"

char *HTGetMime(char *url);

WebGenLinkPtr
new_web_gen_link(void)
{
  WebGenLinkPtr gln = (WebGenLinkPtr)calloc(1, sizeof(WebGenLinkStruct));
  if(!gln) {
      fprintf(stderr,"Out of Memory new_web_gen_link\n");
      abort();
  }
  return gln;
}

void
delete_web_gen_link(WebGenLinkPtr gln)
{
    if(!gln) return;
    free(gln); gln = NULL;
}

void
LOGR_set_type_gen_link(WebGenLinkPtr gln,TypeLink tl)
{
  if(!gln) return;
  gln->tl = tl;
}

TypeLink
LOGR_get_type_gen_link(WebGenLinkPtr gln)
{
  if(!gln) return 0;
  return gln->tl;
}

void
LOGR_set_info_gen_link(WebGenLinkPtr gln,void *ep)
{
  if(!gln) return;
  gln->edgeptr = ep;

}

void *
LOGR_get_info_gen_link(WebGenLinkPtr gln)
{
  if(!gln) return NULL;
  return gln->edgeptr;
}

void
LOGR_set_gen_link_from_name(WebGenLinkPtr gln, char *urls, char *urlt,
			    WebStatPtr wsf,GraphFrame *gf)
{
    void *edgeptr = NULL;
    TypeLink tl=0;
    if(!gln || !urls || !urlt || !wsf || !gf)
	return;
    edgeptr = LOGR_find_info_gen_link(urls,urlt,&tl, wsf,gf);
    gln->edgeptr = edgeptr;
    gln->tl = tl;
}


long
LOGR_get_gen_link_naccesses(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	char *weight = e ? get_weight_edge(e) : NULL;
	long cedge = weight ? atol(weight) : 0;
	return cedge;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_naccesses(oln) : 0;
    }
    return 0;
}

int
LOGR_is_access_gen_link_lower(WebGenLinkPtr u, WebGenLinkPtr v)
{
    long cu = LOGR_get_gen_link_naccesses(u);
    long cv = LOGR_get_gen_link_naccesses(v);
    return (cu < cv); 
}

int
LOGR_is_access_gen_link_bigger(WebGenLinkPtr u,WebGenLinkPtr v)
{
    long cu = LOGR_get_gen_link_naccesses(u);
    long cv = LOGR_get_gen_link_naccesses(v);
    return (cu > cv); 
}

const char *
LOGR_get_gen_url_source(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	return v ? get_label_vertex(v) : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_url(oln) : 0;
    }
    return NULL;
}

const char *
LOGR_get_gen_url_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	return v ? get_label_vertex(v) : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_url(oln) : 0;
    }
    return NULL;
}

const char *
LOGR_get_gen_title_source(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	return v ? get_attribute(v,"title") : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_title(oln) : 0;
    }
    return NULL;
}

const char *
LOGR_get_gen_title_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	return v ? get_attribute(v,"title") : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_title(oln) : 0;
    }
    return NULL;
}

const char *
LOGR_get_gen_mime_source(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	return v ? get_attribute(v,"mime") : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_mime(oln) : 0;
    }
    return NULL;
}

const char *
LOGR_get_gen_mime_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	return v ? get_attribute(v,"mime") : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_mime(oln) : 0;
    }
    return NULL;
}

int
LOGR_get_gen_size_source(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	char *size = v ? (char *)get_attribute(v,"size") : NULL; 
	int nsize = size ? atoi(size) : -1 ;
	return nsize;
	/*char *sw = v ? get_weight_vertex(v) : NULL;
	  return sw ? atoi(sw) : 0;*/
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_size(oln) : 0;
    }
    return 0;
}

int
LOGR_get_gen_size_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	char *size = v ? (char *)get_attribute(v,"size") : NULL; 
	int nsize = size ? atoi(size) : -1 ;
	return nsize;
	/*char *sw = v ? get_weight_vertex(v) : NULL;
	  return sw ? atoi(sw) : 0;*/
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_size(oln) : 0;
    }
    return 0;
}

int
LOGR_get_gen_http_code_source(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	const char *att = v ? get_attribute(v,"code") : NULL;
	int code = att ? atoi(att) : 0;
	return code;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_http_code(oln) : 0;
    }
    return 0;
}

int
LOGR_get_gen_http_code_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	const char *att = v ? get_attribute(v,"code") : NULL;
	int code = att ? atoi(att) : 0;
	return code;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_http_code(oln) : 0;
    }
    return 0;
}

int
LOGR_get_gen_naccesses_source(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	return v ? get_counter(v) : 0;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_naccesses(oln) : 0;
    }
    return NULL;
}

int
LOGR_get_gen_naccesses_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	return v ? get_counter(v) : 0;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_naccesses(oln) : 0;
    }
    return NULL;
}

int
LOGR_get_gen_link_source_num(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_from_vertex(e) : NULL;
	return v ? get_vertex_number(v) : 0;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_source_num(oln) : 0;
    }
    return 0;
}

int
LOGR_get_gen_link_target_num(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	return v ? get_vertex_number(v) : 0;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	return oln ? LOGR_get_link_target_num(oln) : 0;
    }
    return 0;
}

const char *
LOGR_get_gen_link_label(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	return e ? get_label_edge(e) : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	return NULL;
    }
    return NULL;
}

short
LOGR_is_gen_link_name(WebGenLinkPtr gln,char *urls,char *urlt)
{
    char *lurls, *lurlt;

    if(!gln || !urls || !urlt)
	return 0;

    lurls = (char *)LOGR_get_gen_url_source(gln);
    lurlt = (char *)LOGR_get_gen_url_target(gln);

    if(!lurls || !lurlt)
	return 0;

    if(!strcmp(lurls,urls) && !strcmp(lurlt,urlt))
	return 1;

    return 0;
}

short
LOGR_is_gen_links_equal(WebGenLinkPtr gln,WebGenLinkPtr hln)
{
    char *lurls, *lurlt;
    char *hurls, *hurlt;

    if(!gln || !hln)
	return 0;

    lurls = (char *)LOGR_get_gen_url_source(gln);
    lurlt = (char *)LOGR_get_gen_url_target(gln);

    hurls = (char *)LOGR_get_gen_url_source(hln);
    hurlt = (char *)LOGR_get_gen_url_target(hln);

    if(!lurls || !lurlt || !hurls || !hurlt)
	return 0;

    if(!strcmp(lurls,hurls) && !strcmp(lurlt,hurlt))
	return 1;

    return 0;
}

short
LOGR_is_gen_html_target(WebGenLinkPtr gln)
{
    TypeLink tl = gln ? gln->tl : 0;
    char *mime = NULL;
    char *urlt = NULL;

    if(tl == PWEBEDGE)
    {
	PEDGE e = (PEDGE)gln->edgeptr;
	PNODE v = e ? get_to_vertex(e) : NULL;
	urlt = v ? get_label_vertex(v) : NULL;
	mime = v ? (char *)get_attribute(v,"mime") : NULL;
    }
    else if(tl == PWEBINDEPENDENTLINK)
    {
	WebIndependentLinkPtr oln = (WebIndependentLinkPtr)gln->edgeptr;
	urlt = oln ? (char *)LOGR_get_link_target_url(oln) : NULL;
	mime = oln ? (char *)LOGR_get_link_target_mime(oln) : NULL;
    }

    if(!mime)	
    {
	/*char *urls = (char *)LOGR_get_gen_url_source(gln);*/
	mime = (char *)HTGetMime(urlt);
	/*printf("url %s-%s mime %s\n",urls,urlt,mime ? mime : "NULL");*/
    }

    return (mime && (!strcmp(mime,"www/unknown") 
		     || !strcmp(mime,"text/html")));
}

short
LOGR_is_gen_link_independent(WebGenLinkPtr gln)
{
    return (gln ? gln->tl == PWEBINDEPENDENTLINK  : 0);
}


